package cn.zmit.kuxi.activity;

import cn.zmit.kuxi.R;
import cn.zmit.kuxi.entity.NewRecordEntity;
import cn.zmit.kuxi.entity.NewWinnerEnity;
import cn.zmit.kuxi.holder.NewWinnerHolder;
import cn.zmit.kuxi.request.RequestTask;
import cn.zmit.kuxi.widget.PtrHeader;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xdroid.common.utils.ToastUtils;
import com.xdroid.functions.holder.ListViewDataAdapter;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;
import com.xdroid.widget.loadmore.LoadMoreContainer;
import com.xdroid.widget.loadmore.LoadMoreDefaultFooterView;
import com.xdroid.widget.loadmore.LoadMoreHandler;
import com.xdroid.widget.loadmore.LoadMoreListViewContainer;
import com.xdroid.widget.ptr.PtrDefaultHandler;
import com.xdroid.widget.ptr.PtrFrameLayout;
import com.xdroid.widget.ptr.PtrHandler;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

/** * @author @parameter * @return */
public class NewWinnerActivity extends BaseActivity {
	@ViewInject(R.id.listView_winner)
	private ListView listView_win;
	private ListViewDataAdapter<NewWinnerEnity> newWindAdapter;
	private String flag = null;
	// 下拉刷新
	@ViewInject(R.id.ptr_view)
	private PtrFrameLayout ptr_view;
	private PtrHeader ptrHeader;

	/**
	 * pull up
	 * 
	 * @author Joash_Yan
	 */
	private int mPage = 1;
	@ViewInject(R.id.load_more_list_view_container)
	private LoadMoreListViewContainer mLoadMoreListViewContainer;
	LoadMoreDefaultFooterView mLoadView;

	/**
	 * 初始化上拉刷新，分页显示
	 */
	private void initLoadMore() {
		mLoadView = mLoadMoreListViewContainer.useDefaultFooterRe(); // 使用默认的FooterView
		mLoadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {

			@Override
			public void onLoadMore(LoadMoreContainer loadMoreContainer) {
				if (!mLoadMoreListViewContainer.isLoadError()) {
					mPage++;
				}
				getDate();
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		View view = initTitleBar();
		setTitleView(view);
		setContentView(R.layout.activity_winner);
		ViewUtils.inject(this);
		initView();
		initPtr();
		initLoadMore();
		Button btn_gotop = (Button) findViewById(R.id.btn_gotop);
		btn_gotop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				listView_win.setSelection(0);
				initPtr();
			}
		});
	}

	private void getDate() {
		RequestTask.getInstance().getNewStrecord(this, mPage + "",
				new OnRequestListener());
	}

	/**
	 * 初始化下拉刷新
	 */
	private void initPtr() {
		/* 创建自定义刷新头部view */
		ptrHeader = new PtrHeader(this);
		/* 设置刷新头部view */
		ptr_view.setHeaderView(ptrHeader);
		/* 设置回调 */
		ptr_view.addPtrUIHandler(ptrHeader);
		/* 横向拉动时，禁止下拉 */
		ptr_view.disableWhenHorizontalMove(true);
		// 设置监听
		ptr_view.setPtrHandler(new PtrHandler() {

			@Override
			public void onRefreshBegin(PtrFrameLayout frame) {
				frame.postDelayed(new Runnable() {

					@Override
					public void run() {
						newWindAdapter.removeAll();
						mPage = 1;
						getDate();
					}

				}, 500);
			}

			@Override
			public boolean checkCanDoRefresh(PtrFrameLayout frame,
					View content, View header) {
				return PtrDefaultHandler.checkContentCanBePulledDown(frame,
						listView_win, header);
			}
		});
		/* 延时100hao秒,自动刷新 */
		ptr_view.postDelayed(new Runnable() {

			@Override
			public void run() {
				ptr_view.autoRefresh();
			}
		}, 100);
	}

	// 初始化
	private void initView() {
		newWindAdapter = new ListViewDataAdapter<NewWinnerEnity>();
		newWindAdapter.setViewHolderClass(this, NewWinnerHolder.class);
		listView_win.setAdapter(newWindAdapter);
	}

	// qingqiu
	private class OnRequestListener extends OnRequestListenerAdapter<Object> {
		private String next_gp_id;

		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			// TODO Auto-generated method stub
			super.onDone(request, response, dataType);

			JsonData jsonData = JsonData.create(response);

			if (response != null) {
				JsonData data = jsonData.optJson("newest_record");
				int nowtime = jsonData.optInt("now_time");
				Boolean mIsEmpty = false; // 是否是空数据
				Boolean mHasMore = false; // 是否有更多数据待加载
				if (data.length() > 0) {
					if (data.length() == 8) {
						mHasMore = true; // 有更多数据
					} else if (data.length() < 8) {
						mIsEmpty = false; // 有数据
						mHasMore = false; // 无更多数据
					}
					for (int i = 0; i < data.length(); i++) {
						JsonData itemDate = data.optJson(i);
						String join_count = itemDate.optString("buy_count");
						String title = itemDate.optString("phase");
						String user = itemDate.optString("phone");
						String goodsName = itemDate.optString("name");
						int time = itemDate.optInt("unix_announceOn");
						String annoutime = itemDate.optString("announceOn");
						String luckNum = itemDate.optString("lucky_number");
						String pic = itemDate.optString("pic");
						String goods_id = itemDate.optString("goods_id");
						next_gp_id = itemDate.optString("next_gp_id");
						String gp_id = itemDate.optString("gp_id");
						String price = itemDate.optString("price");

						NewWinnerEnity newWinnerEnity = new NewWinnerEnity(
								join_count, title, goodsName, user, time,
								nowtime, luckNum, pic, goods_id, gp_id, price,
								next_gp_id, annoutime);
						newWindAdapter.append(newWinnerEnity);
					}
				} else {
					mIsEmpty = true; // 空数据
					mHasMore = false; // 无更多数据
				}
				mLoadMoreListViewContainer.loadMoreFinish(mIsEmpty, mHasMore); // 判断是否加载更多
				newWindAdapter.notifyDataSetChanged();
			} else {
				ToastUtils.show(context, "信息获取失败");
			}
			ptr_view.refreshComplete();
		}

		@Override
		public void onRequestFailed(String error) {
			super.onRequestFailed(error);
			ptr_view.refreshComplete();
		}

		@Override
		public void onRequestPrepare() {
			super.onRequestPrepare();
		}
	}

	/**
	 * 初始化titlebar
	 */
	private View initTitleBar() {
		View view = View.inflate(context, R.layout.view_newwin_titlebar, null);
		TextView tv_titlebar_title = (TextView) view
				.findViewById(R.id.tv_titlebar_title);
		tv_titlebar_title.setText("最新喜报");
		return view;
	}

}
